﻿
''' <summary>
''' 門市基礎資料
''' </summary>
''' <remarks>
''' Create Date:2008-04-21
''' Create Person:huyang
''' </remarks>
Public Class Frm_CS_ShopInformation

    Implements IDServer.IDServer.IDServer

#Region "Variabled"

#Region "System Variabled Define"


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

    Public action_help As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Public FindBool As Boolean = True

#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

    Public Fields As String

#End Region

#End Region

#Region "Methods"

#Region "Initilize"

    Public Function ReturnCaption(ByVal Index As Integer) As Hashtable
        Dim Hash As New Hashtable

        Select Case Index

            Case 1 ''門市基本信息表:CS_ShopInformation
                Hash.Add("ShopCode", "編號")
                Hash.Add("ShortCode", "代碼")
                Hash.Add("ShopName", "門市名稱")
                Hash.Add("StoreCode", "倉號")
                Hash.Add("TradeTime", "營業時間")
                Hash.Add("SaleRule", "销售单規則")
                Hash.Add("ShopArea", "門店位置")
                Hash.Add("Area", "面積")
                Hash.Add("Address", "地址")
                Hash.Add("EnAddress", " detial address")
                Hash.Add("Remark", "備注")
            Case 2
                ''手寫單號前綴:CS_ShopHander

                Hash.Add("ItemNo", "序號")
                Hash.Add("fTypeName", "類型")
                Hash.Add("fHandCode", "前綴代碼")

            Case 3
                ''門市聯系電話:CS_ShopContactPhone
                Hash.Add("ItemNo", "序號")
                Hash.Add("DeptName", "部門名稱")
                Hash.Add("Telphone", "電話")
                Hash.Add("Fax", "傳真")
                Hash.Add("Remark", "備注")
            Case 4
                ''門市管理人員信息表:CS_ShopManage
                Hash.Add("ItemNo", "序號")
                Hash.Add("fTypeNo", "類型")
                Hash.Add("ManageName", "姓名")
                Hash.Add("Telphone", "電話")
                Hash.Add("CellPhone", "移動電話")

        End Select

        Return Hash
    End Function

#End Region

#Region "ExecFind"

    Private Function ReturnWhereExp() As String
        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql = "   where 1<>1 "
        Else
            sql = "   where 1=1 "
            If Me.txt_ShopCode.Text.Trim <> "" Then
                sql &= "   and   ShopCode like '%" & Replace_Text(Me.txt_ShopCode.Text.Trim) & "%'"
            End If

            If Me.txt_ShortCode.Text.Trim <> "" Then
                sql &= "  and ShortCode  like  '%" & Replace_Text(Me.txt_ShortCode.Text.Trim) & "%'"
            End If

            If Me.txt_ShopName.Text.Trim <> "" Then
                sql &= "   and ShopName like '%" & Replace_Text(Me.txt_ShopName.Text.Trim) & "%'"
            End If

            If Me.txt_ShopArea.Text.Trim <> "" Then
                sql &= "  and ShopArea like '%" & Replace_Text(Me.txt_ShopArea.Text.Trim) & "%'"
            End If

        End If

        Return sql
    End Function

    Private Sub ExecFind()
        Me.Cursor = Cursors.WaitCursor
        Dim sql As String = ""

        Dim whereExp As String = Me.ReturnWhereExp()

        sql &= " SELECT [Id]"
        sql &= " ,[ShopCode]"
        sql &= " ,[ShortCode]"
        sql &= " ,[ShopName]"
        sql &= " ,[StoreCode]"
        sql &= " ,[TradeTime]"
        sql &= " ,[ShopArea]"
        sql &= " ,[SaleRule]"
        sql &= " ,[Area]"
        sql &= " ,[Address]"
        sql &= " ,[EnAddress]"
        sql &= " ,[Remark]"
        sql &= "FROM [Test].[dbo].[CS_ShopInformation]    " & whereExp

        Try
            Dim Dtab As New DataTable()
            Dtab = Exec(sql, E_ExecType.DataTable)

            Me.DGV_Master.DataSource = Dtab
            BindDataGridViewCaption(Me.DGV_Master, Me.ReturnCaption(1))
            FullFormatDataGridView(Me.DGV_Master, False, False)
            Me.DGV_Master.SelectionMode = DataGridViewSelectionMode.CellSelect

            DGV_Master.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            Dim isFind As Boolean = Me.DGV_Master.RowCount > 0

            If Me.fLoadBool = True AndAlso FindBool = True Then
                Me.SetButton(ButtonType.Browse, isFind)
            End If

            If isFind = False Then
                FillDetailGrid("", 0)
            Else
                Me.DGV_Master.CurrentCell = Me.DGV_Master.Rows(0).Cells("ShopCode")
                Me.DGV_Master.CurrentCell.Selected = True
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    Private Sub FillDetailGrid(ByVal shopCode As String, ByVal Index As Integer)
        Dim ds As New DataSet
        Dim sql As String = ""

        sql &= " select Id,ShopCode,ItemNo,DeptCode,DeptName,Telphone,Fax,Remark from CS_ShopContactPhone    where ShopCode='" & shopCode & "'   order by ItemNo " & vbCrLf
        sql &= " select Id,ShopCode,ItemNo,fTypeName,fHandCode from CS_ShopHander where ShopCode='" & shopCode & "'     order by ItemNo" & vbCrLf
        sql &= " select Id,ShopCode,ItemNo,fTypeNo,ManageName,Telphone,CellPhone,Remark from CS_ShopManage where ShopCode='" & shopCode & "'    order by ItemNo" & vbCrLf

        Try
            ds = Exec(sql, E_ExecType.DataSet)

            Select Case Index
                Case 0
                    Me.dgTelphone.DataSource = ds.Tables(0)
                    BindDataGridViewCaption(Me.dgTelphone, Me.ReturnCaption(3))
                    FullFormatDataGridView(Me.dgTelphone, False, False)
                    Me.dgTelphone.SelectionMode = DataGridViewSelectionMode.CellSelect
                    dgTelphone.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

                    Me.dgWrite.DataSource = ds.Tables(1)
                    BindDataGridViewCaption(Me.dgWrite, Me.ReturnCaption(2))
                    FullFormatDataGridView(Me.dgWrite, False, False)
                    Me.dgWrite.SelectionMode = DataGridViewSelectionMode.CellSelect
                    dgWrite.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

                    Me.dgManagePsn.DataSource = ds.Tables(2)
                    BindDataGridViewCaption(Me.dgManagePsn, Me.ReturnCaption(4))
                    FullFormatDataGridView(Me.dgManagePsn, False, False)
                    Me.dgManagePsn.SelectionMode = DataGridViewSelectionMode.CellSelect
                    dgManagePsn.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

                Case 1
                    Me.dgv_Telphone.DataSource = ds.Tables(0)
                    BindDataGridViewCaption(Me.dgv_Telphone, Me.ReturnCaption(3))
                    FullFormatDataGridView(Me.dgv_Telphone, False, False)
                    Me.dgv_Telphone.SelectionMode = DataGridViewSelectionMode.CellSelect
                    dgv_Telphone.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

                    Me.dgv_HandWrite.DataSource = ds.Tables(1)
                    BindDataGridViewCaption(Me.dgv_HandWrite, Me.ReturnCaption(2))
                    FullFormatDataGridView(Me.dgv_HandWrite, False, False)
                    Me.dgv_HandWrite.SelectionMode = DataGridViewSelectionMode.CellSelect
                    dgv_HandWrite.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

                    Me.dgv_Manage.DataSource = ds.Tables(2)
                    BindDataGridViewCaption(Me.dgv_Manage, Me.ReturnCaption(4))
                    FullFormatDataGridView(Me.dgv_Manage, False, False)
                    Me.dgv_Manage.SelectionMode = DataGridViewSelectionMode.CellSelect
                    dgv_Manage.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            End Select
        Catch ex As Exception

        End Try
        


    End Sub

#End Region

#Region "SetEnabled"


    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type">操作類型</param>
    ''' <param name="IsFind">是否是流覽</param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try

            Select Case type
                Case ButtonType.Add
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                        .ToolStripCheck.Enabled = False
                        .ToolStripComplete.Enabled = False
                        .ToolStripUnCheck.Enabled = False
                    End With

                Case ButtonType.Cancel
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub



#End Region

#Region "AddHandler Events"

    ''' <summary>
    ''' KeyDown Event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            Select Case e.KeyCode
                Case Keys.Enter
                    If e.Shift = True Then
                        If TypeOf sender Is ComboBox Then
                            If CType(sender, ComboBox).DroppedDown = True Then
                                CType(sender, ComboBox).DroppedDown = False
                            End If
                        End If
                        Me.ProcessTabKey(False)
                        SendKeys.Send("{F4}")
                    Else
                        If TypeOf sender Is ComboBox Then
                            If CType(sender, ComboBox).DroppedDown = True Then
                                CType(sender, ComboBox).DroppedDown = False
                            End If
                        End If

                        Me.ProcessTabKey(True)
                        SendKeys.Send("{F4}")

                    End If
            End Select
        Catch ex As Exception
            MsgBox("TextBox_KeyDown:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 添加自定義事件
    ''' </summary>
    ''' <param name="Pans"></param>
    ''' <remarks></remarks>
    Private Sub AddHandlers(ByVal Pans() As Control)
        Try
            For Each Pan As Control In Pans
                For Each Ctrl As Control In Pan.Controls
                    If TypeOf Ctrl Is TextBox _
                       OrElse TypeOf Ctrl Is ComboBox _
                       OrElse TypeOf Ctrl Is DateTimePicker Then
                        AddHandler Ctrl.KeyDown, AddressOf TextBox_KeyDown
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox("AddHandlers:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

#End Region

#Region "FillDetail"

    Private Sub FillDetail(ByVal shopCode As String)

        Dim Dtab As DataTable = DirectCast(Me.DGV_Master.DataSource, DataTable)
        Dim DRow() As DataRow = Dtab.Select("ShopCode='" & shopCode & "'")
        If DRow.Length > 0 Then
            Me.txtShopCode.Text = DRow(0).Item("ShopCode") & ""
            Me.txtShortCode.Text = DRow(0).Item("ShortCode") & ""
            Me.txtShopName.Text = DRow(0).Item("ShopName") & ""
            Me.txtTradeTime.Text = DRow(0).Item("TradeTime") & ""
            Me.txtShopArea.Text = DRow(0).Item("ShopArea") & ""
            Me.txtSaleRule.Text = DRow(0).Item("SaleRule") & ""
            Me.txtArea.Text = DRow(0).Item("Area") & ""
            Me.txtStoreCode.Text = DRow(0).Item("StoreCode") & ""
            Me.txtAddress.Text = DRow(0).Item("Address") & ""
            Me.txtEnAddress.Text = DRow(0).Item("EnAddress") & ""
            Me.txtRemark.Text = DRow(0).Item("Remark") & ""
        Else
            Me.txtShopCode.Text = ""
            Me.txtShortCode.Text = ""
            Me.txtShopName.Text = ""
            Me.txtTradeTime.Text = ""
            Me.txtShopArea.Text = ""
            Me.txtSaleRule.Text = ""
            Me.txtArea.Text = ""
            Me.txtStoreCode.Text = ""
            Me.txtAddress.Text = ""
            Me.txtEnAddress.Text = ""
            Me.txtRemark.Text = ""
        End If

        Me.FillDetailGrid(shopCode, 1)

    End Sub

#End Region

#Region "SaveData"

    Private Function Validated1() As Boolean
        Me.dgv_Telphone.EndEdit()
        Me.dgv_Telphone.Update()
        Me.dgv_Telphone.Refresh()

        Me.dgv_HandWrite.EndEdit()
        Me.dgv_HandWrite.Update()
        Me.dgv_HandWrite.Refresh()

        Me.dgv_Manage.EndEdit()
        Me.dgv_Manage.Update()
        Me.dgv_Manage.Refresh()

        If Me.txtShopName.Text.Trim = "" Then
            MessageBox.Show("門店名稱不能為空。", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Me.txtShopName.Focus()
            Return False
        End If
        Return True
    End Function

    Private Function SaveData() As Boolean
        Dim bSuccess As Boolean = False

        If Me.Validated1() = False Then
            Return False
        End If

        Dim ShopCode As String = Me.txtShopCode.Text.Trim
        Dim ShortCode As String = Me.txtShortCode.Text.Trim
        Dim ShopName As String = Me.txtShopName.Text.Trim
        Dim StoreCode As String = Me.txtStoreCode.Text.Trim
        Dim TradeTime As String = Me.txtTradeTime.Text.Trim
        Dim ShopArea As String = Me.txtShopArea.Text.Trim
        Dim SaleRule As String = Me.txtSaleRule.Text.Trim
        Dim Area As String = Me.txtArea.Text.Trim
        Dim Address As String = Me.txtAddress.Text.Trim
        Dim EnAddress As String = Me.txtEnAddress.Text.Trim
        Dim Remark As String = Me.txtRemark.Text.Trim

        Dim Sql As System.Text.StringBuilder = New System.Text.StringBuilder()

        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
            ShopCode = Exec("select Isnull(Max(ShopCode),0) from CS_ShopInformation", E_ExecType.ExecuteScalar) & ""
            ShopCode = (Val(ShopCode) + 1).ToString("0000")
            Me.txtShopCode.Text = ShopCode
            Sql.Append("INSERT INTO [Test].[dbo].[CS_ShopInformation]" & vbCrLf)
            Sql.Append("           ([ShopCode]")
            Sql.Append("           ,[ShortCode]")
            Sql.Append("           ,[ShopName]")
            Sql.Append("           ,[StoreCode]")
            Sql.Append("           ,[TradeTime]")
            Sql.Append("           ,[ShopArea]")
            Sql.Append("           ,[SaleRule]")
            Sql.Append("           ,[Area]")
            Sql.Append("           ,[Address]")
            Sql.Append("           ,[EnAddress]")
            Sql.Append("           ,[Remark])")
            Sql.Append("values(" & vbCrLf)
            Sql.Append("'" & ShopCode & "','" & ShortCode & "','" & ShopName & "','" & StoreCode & "','" & TradeTime & "','" & ShopArea & "','" & SaleRule & "','" & Area & "','" & Address & "','" & EnAddress & "','" & Remark & "'")
            Sql.Append(")" & vbCrLf)

        Else
            Sql.Append("UPDATE [Test].[dbo].[CS_ShopInformation]" & vbCrLf)

            Sql.Append("  SET    [ShortCode] = '" & ShortCode & "'" & vbCrLf)
            Sql.Append("            ,[ShopName] ='" & ShopName & "'")
            Sql.Append("            ,[StoreCode] ='" & StoreCode & "'")
            Sql.Append("            ,[TradeTime] ='" & TradeTime & "'")
            Sql.Append("            ,[ShopArea] ='" & ShopArea & "'")
            Sql.Append("            ,[SaleRule] ='" & SaleRule & "'")
            Sql.Append("            ,[Area] ='" & Area & "'")
            Sql.Append("            ,[Address] ='" & Address & "'")
            Sql.Append("            ,[EnAddress] ='" & EnAddress & "'")
            Sql.Append("            ,[Remark] ='" & Remark & "'" & vbCrLf)
            Sql.Append("   WHERE  ShopCode='" & ShopCode & "'" & vbCrLf)

            Sql.Append("Delete CS_ShopContactPhone where ShopCode='" & ShopCode & "' " & vbCrLf)
            Sql.Append("Delete CS_ShopHander where ShopCode='" & ShopCode & "' " & vbCrLf)
            Sql.Append("Delete CS_ShopManage where ShopCode='" & ShopCode & "'  " & vbCrLf)

        End If


        Dim Telphonedb As DataTable = DirectCast(Me.dgv_Telphone.DataSource, DataTable)
        Telphonedb.AcceptChanges()

        If Telphonedb.Rows.Count > 0 Then

            Sql.Append("Insert Into CS_ShopContactPhone(ShopCode,ItemNo,DeptCode,DeptName,Telphone,Fax,Remark)" & vbCrLf)

            Dim I As Integer = 1
            Dim Textstr As String = ""
            For Each row As DataRow In Telphonedb.Rows

                If I > 1 Then
                    Textstr &= "  Union all " & vbCrLf
                End If
                Textstr &= "  select  '" & ShopCode & "'," & I & ",Null,'" & row("DeptName") & "','" & row("Telphone") & "','" & row("Fax") & "','" & row("Remark") & "' " & vbCrLf
                I += 1
            Next

            Sql.Append(Textstr)

        End If

        Dim Handerdb As DataTable = DirectCast(Me.dgv_HandWrite.DataSource, DataTable)
        Handerdb.AcceptChanges()

        If Handerdb.Rows.Count > 0 Then

            Sql.Append("Insert Into CS_ShopHander(ShopCode,ItemNo,fTypeName,fHandCode) " & vbCrLf)
            Dim I As Integer = 1
            Dim Textstr As String = ""
            For Each row As DataRow In Handerdb.Rows

                If I > 1 Then
                    Textstr &= "  Union all " & vbCrLf
                End If
                Textstr = "  select '" & ShopCode & "'," & I & ",'" & row("fTypeName") & "','" & row("fHandCode") & "'" & vbCrLf
                I += 1
            Next
            Sql.Append(Textstr)
        End If

        Dim Managedb As DataTable = DirectCast(Me.dgv_Manage.DataSource, DataTable)
        Managedb.AcceptChanges()

        If Managedb.Rows.Count > 0 Then

            Sql.Append("Insert Into CS_ShopManage(ShopCode,ItemNo,fTypeNo,ManageName,Telphone,CellPhone,Remark)" & vbCrLf)

            Dim I As Integer = 1
            Dim Textstr As String = ""
            For Each row As DataRow In Managedb.Rows
                If I > 1 Then
                    Textstr &= "  Union all " & vbCrLf
                End If
                Textstr &= " select '" & ShopCode & "'," & I & ",'" & row("fTypeNo") & "','" & row("ManageName") & "','" & row("Telphone") & "','" & row("CellPhone") & "','" & row("Remark") & "' "
                I += 1
            Next
            Sql.Append(Textstr)
        End If

        Try
            Exec(Sql.ToString(), E_ExecType.ExecuteNonQuery, True)
            FullFormatDataGridView(Me.dgv_Telphone)
            FullFormatDataGridView(Me.dgv_HandWrite)
            FullFormatDataGridView(Me.dgv_Manage)
            bSuccess = True
        Catch ex As Exception
            bSuccess = False
            MessageBox.Show(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.ErrAdd, My.resources.ErrEdit), Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        If bSuccess = True Then
            MessageBox.Show(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.Addsuccess, My.resources.EditSuccess), Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        Me.SetButton(ButtonType.Browse, True)
        Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
        cs.SetControlsEnabled(False)
        Return bSuccess
    End Function

#End Region

#End Region

#Region "Events"

#Region "Form Load"

    Private Sub Frm_CS_ShopInformation_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.fLoadBool = False
            Me.txt_ShopCode.Focus()
            Me.AddHandlers(New Control() {Panel1, GroupBox2})
            addTextBoxEvents()
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(False)
            Dim imeMode As clsIme = New clsIme()
            imeMode.AddHandler_ImeMode(New Control() {Panel1, GroupBox2})
            ExecFind()
            SetGridCaptionVisible(Me.DGV_Master, Fields, "Frm_CS_ShopInformation")
            Me.fLoadBool = True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#Region "Button"

    Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click

        ExecFind()

    End Sub

#End Region

#Region "DataGridView"

    Private Sub DGV_Master_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Master.CellDoubleClick
        Me.TabControl1.SelectedIndex = 1
    End Sub

    Private Sub DGV_Master_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGV_Master.CurrentCellChanged
        Dim shopCode As String = ""
        If Not Me.DGV_Master.CurrentCell Is Nothing Then
            shopCode = Me.DGV_Master.CurrentRow.Cells("ShopCode").Value & ""
        End If
        Me.FillDetailGrid(shopCode, 0)
    End Sub



#End Region

#Region "TabControl"

    Private Sub TabControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TabControl1.KeyDown
        Select Case e.KeyCode
            Case Keys.S
                If e.Control = True Then
                    If (ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit) Then
                        MainForm.ToolStripSave.PerformClick()
                    End If
                End If
        End Select
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Try
            If Me.TabControl1.SelectedIndex = 1 Then
                Dim shopCode As String = ""
                If Not Me.DGV_Master.CurrentRow Is Nothing Then
                    shopCode = Me.DGV_Master.CurrentRow.Cells("ShopCode").Value & ""
                End If
                Me.FillDetail(shopCode)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting

        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
            e.Cancel = True
        End If

    End Sub



#End Region

#End Region

#Region "Interface"


    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            Me.FillDetail("")
            ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(True)
            Me.txtShopCode.ReadOnly = True
            Me.txtShortCode.Focus()
            Me.SetButton(ButtonType.Add, False)


            Dim tb1 As DataTable
            tb1 = DirectCast(dgv_Telphone.DataSource, DataTable)
            Dim row1 As DataRow
            row1 = tb1.NewRow()
            tb1.Rows.Add(row1)
            tb1.AcceptChanges()


            Dim tb2 As DataTable
            tb2 = DirectCast(dgv_HandWrite.DataSource, DataTable)
            Dim row2 As DataRow
            row2 = tb2.NewRow()
            tb2.Rows.Add(row2)
            tb2.AcceptChanges()

            Dim tb3 As DataTable
            tb3 = DirectCast(dgv_Manage.DataSource, DataTable)
            Dim row3 As DataRow
            row3 = tb3.NewRow()
            tb3.Rows.Add(row3)
            tb3.AcceptChanges()

            FullFormatDataGridView(Me.dgv_Telphone, True, True, True)
            Me.dgv_Telphone.Columns("ItemNo").ReadOnly = True
            FullFormatDataGridView(Me.dgv_HandWrite, True, True, True)
            Me.dgv_HandWrite.Columns("ItemNo").ReadOnly = True
            FullFormatDataGridView(Me.dgv_Manage, True, True, True)
            Me.dgv_Manage.Columns("ItemNo").ReadOnly = True

            If Me.dgv_HandWrite.Columns("Id").Visible = True Then
                Me.dgv_HandWrite.Columns("Id").Visible = False
            End If
            If Me.dgv_Manage.Columns("Id").Visible = True Then
                Me.dgv_Manage.Columns("Id").Visible = False
            End If
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            Dim shopCode As String = ""
            Dim isFind As Boolean = False
            If Not Me.DGV_Master.CurrentRow Is Nothing Then
                shopCode = Me.DGV_Master.CurrentRow.Cells("ShopCode").Value & ""
                isFind = True
            End If

            FullFormatDataGridView(Me.dgv_Telphone)
            FullFormatDataGridView(Me.dgv_HandWrite)
            FullFormatDataGridView(Me.dgv_Manage)

            Me.FillDetail(shopCode)
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(False)
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            Me.SetButton(ButtonType.Browse, isFind)

            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If DeleteEnable = False Then
                MessageBox.Show("對不起，您沒有刪除權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.DGV_Master.CurrentRow Is Nothing Then Return False

            If MessageBox.Show(My.resources.ConfirmDelete, Msg.CompanyName, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) <> DialogResult.OK Then Return False

            Dim ShopCode As String = Me.DGV_Master.CurrentRow.Cells("ShopCode").Value & ""

            Dim sql As String = ""
            sql &= " Delete CS_ShopContactPhone where ShopCode='" & ShopCode & "' " & vbCrLf
            sql &= " Delete CS_ShopHander where ShopCode='" & ShopCode & "' " & vbCrLf
            sql &= " Delete CS_ShopManage where ShopCode='" & ShopCode & "' " & vbCrLf
            sql &= " Delete CS_ShopInformation where ShopCode='" & ShopCode & "' " & vbCrLf

            Exec(sql, E_ExecType.ExecuteNonQuery, True)
            If Me.TabControl1.SelectedIndex = 1 Then
                Me.FillDetail("")
            End If
            Dim Dtab As DataTable = DirectCast(Me.DGV_Master.DataSource, DataTable)
            Dim DRow() As DataRow = Dtab.Select("ShopCode='" & ShopCode & "'")
            If DRow.Length > 0 Then
                DRow(0).Delete()
            End If
            Dtab.AcceptChanges()
            Me.DGV_Master.Refresh()
            Dim IsFind As Boolean = False
            If Me.TabControl1.SelectedIndex = 0 Then
                If Me.DGV_Master.RowCount > 0 Then
                    Me.DGV_Master.CurrentCell = Me.DGV_Master.Rows(Me.DGV_Master.RowCount - 1).Cells("ShopCode")
                    Me.DGV_Master.CurrentCell.Selected = True
                    IsFind = True
                End If
                Me.SetButton(ButtonType.Browse, IsFind)
            Else
                Me.SetButton(ButtonType.Browse, False)
            End If
            MessageBox.Show(My.resources.DeleteSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(My.resources.ErrDelete, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
        Return True
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show("對不起，您沒有修改權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
            Me.SetButton(ButtonType.Edit, False)
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(True)
            Me.txtShopCode.ReadOnly = True
            FullFormatDataGridView(Me.dgv_Telphone, True, True, True)
            Me.dgv_Telphone.Columns("ItemNo").ReadOnly = True
            FullFormatDataGridView(Me.dgv_HandWrite, True, True, True)
            Me.dgv_HandWrite.Columns("ItemNo").ReadOnly = True
            FullFormatDataGridView(Me.dgv_Manage, True, True, True)
            Me.dgv_Manage.Columns("ItemNo").ReadOnly = True
            Me.txtShopName.Focus()
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter
        Try
            Dim Frm_Filter As New Frm_Filter
            Me.Cursor = Cursors.WaitCursor
            Frm_Filter.LoadHeaderCaption(Fields, "Frm_CS_ShopInformation")
            Frm_Filter.ShowDialog()
            Me.Cursor = Cursors.Default
            ReBindGridCaption(Me.DGV_Master, "Frm_CS_ShopInformation")
        Catch ex As Exception
            MsgBox("ActionFilter:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        Dim bSuccess As Boolean = False
        bSuccess = SaveData()
        Return bSuccess
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region



    Private Sub addTextBoxEvents()

        For Each col As Control In Me.GroupBox1.Controls
            If TypeOf col Is TextBox Then
                AddHandler col.KeyDown, AddressOf txt_KeyDown
            End If
        Next
    End Sub

    Private Sub txt_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            Me.btnFind.PerformClick()
        End If
    End Sub
End Class
